import 'package:flutter/material.dart';
import 'package:whale/helper/utils/um/event.dart';
import 'package:whale/helper/utils/um/um_man.dart';
import 'package:whale/helper/utils/util.dart';
import 'package:whale/pages/msg_page/index.dart';

import '../home/main/index.dart';
import '../me/view.dart';
import 'index.dart';

class AppTabController extends GetxController {
  AppTabController();

  final state = AppTabState();

  void clickTab(int index) {
    if (state.currentIndex.value == index) {
      state.currentIndex.value == index;
      if (index != 3) {
        state.isRefreshing.value = true;
      }

      if (state.pageController.page != index) {
        state.pageController.jumpToPage(index);
      }
      updateRefresh();
      return;
    }
    state.isRefreshing.value = false;
    state.currentIndex.value = index;
    state.pageController.jumpToPage(index);
  }

  Widget currPage(int index) {
    switch (index) {
      case 1:
        return state.page3 ??= KeepAlivePage(child: MsgPagePage());
      case 2:
        // UmMan().onEvent(event: UmEvent.tabMe);
        return state.page2 ??= KeepAlivePage(child: MePage());
      default:
        // UmMan().onEvent(event: UmEvent.tabHome);
        return state.page1 ??= KeepAlivePage(child: HomePage());
    }
  }

  void setupData() {
    state.items = [
      TabModel(
        icon: 'tab_1_n.png',
        activeIcon: 'tab_1_y.png',
        title: '首页'.ls,
      ),
      TabModel(
        icon: 'tab_2_n.png',
        activeIcon: 'tab_2_y.png',
        title: '刷新'.ls,
      ),
      TabModel(
        icon: 'tab_4_n.png',
        activeIcon: 'tab_4_y.png',
        title: '我的'.ls,
      ),
    ];
  }

  void updateRefresh() {
    update(['refresh']);
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    setupData();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
